package com.ljkj.cordial.chat.widget;

import android.content.Context;
import android.support.design.chip.Chip;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.CompoundButton;
import android.widget.LinearLayout;

import com.ljkj.cordial.chat.R;

/**
 * 开关切换
 */
public class ChipSwitch extends LinearLayout {
    private Chip chip;

    public ChipSwitch(Context context) {
        super(context);
        init();
    }

    public ChipSwitch(Context context, AttributeSet attrs) {
        super(context, attrs);
        init();
    }

    public ChipSwitch(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init();
    }


    private void init() {
        View v = LayoutInflater.from(getContext()).inflate(R.layout.widget_chip_switch, this);
        chip = v.findViewById(R.id.chip);
        chip.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                if (isChecked) {
                    chip.setCloseIconVisible(false);
                    chip.setText("OFF");
                    chip.setChipBackgroundColorResource(R.color.item_blue);
                    chip.setRippleColorResource(R.color.item_blue);
                } else {
                    chip.setCloseIconVisible(true);
                    chip.setText("ON ");
                    chip.setChipBackgroundColorResource(R.color.gray_500);
                    chip.setRippleColorResource(R.color.gray_500);
                }
            }
        });
        chip.setOnCloseIconClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                chip.setChecked(true);
            }
        });
    }

}
